﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using GTS.MiniBox.Entity;
using Pub.Class;

namespace GTS.MiniBox.Helper {
    /// <summary>
    /// 操作类
    /// 
    /// 修改纪录
    ///     2012-07-25 版本：1.0 系统自动创建此类
    /// 
    /// </summary>
    public partial class ChannelRuleHelper {
        /// <summary>
        /// 缓存多少秒 x 5
        /// </summary>
        public static int cacheSeconds = 1440;
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="channelRule">实体类</param>
        /// <param name="delCache">添加成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>添加是否成功</returns>
        public static bool Insert(ChannelRule channelRule, string dbkey = "", string[] delCache = null) {
            string strSql = new SQL().Insert(ChannelRule._)
                .Value(ChannelRule._CstCode, channelRule.CstCode)
                .Value(ChannelRule._CityCode, channelRule.CityCode)
                .Value(ChannelRule._ChannelID, channelRule.ChannelID)
                .Value(ChannelRule._Type, channelRule.Type)
                .Value(ChannelRule._Status, channelRule.Status)
                .Value(ChannelRule._StatusDate, channelRule.StatusDate)
                .Value(ChannelRule._StatusResult, channelRule.StatusResult)
                .Value(ChannelRule._CreateTime, channelRule.CreateTime)
                .Value(ChannelRule._MaxSendCount, channelRule.MaxSendCount)
                .Value(ChannelRule._ManualOrderBy, channelRule.ManualOrderBy)
                .Value(ChannelRule._OrderNum, channelRule.OrderNum)
                .Value(ChannelRule._OrderNum2, channelRule.OrderNum2)
                .Value(ChannelRule._SuccessRate, channelRule.SuccessRate)
                .Value(ChannelRule._SendCount, channelRule.SendCount)
                .Value(ChannelRule._SendUser, channelRule.SendUser)
                .Value(ChannelRule._SendMoney, channelRule.SendMoney)
                .Value(ChannelRule._SpCount, channelRule.SpCount)
                .Value(ChannelRule._SpUser, channelRule.SpUser)
                .Value(ChannelRule._SpMoney, channelRule.SpMoney)
                .Value(ChannelRule._UpdateTime, channelRule.UpdateTime)
                .Value(ChannelRule._Ranking, channelRule.Ranking)
                .ToString();
            bool istrue = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) == 1 : Data.Pool(dbkey).ExecSql(strSql) == 1;
            if (delCache.IsNull()) return istrue;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.ChannelRuleCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.ChannelRuleCache_" + s + ")" : s);
            }
            return istrue;
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="channelRule">实体类</param>
        /// <returns>添加是否成功</returns>
        public static bool Insert(ChannelRule channelRule, string dbkey = "") {
            return Insert(channelRule, dbkey, null);
        }
        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="channelRule">实体类</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(ChannelRule channelRule, string dbkey = "", string where = "", string[] delCache = null) {
            if (channelRule.CstCode.IsNull()  && channelRule.CityCode.IsNullEmpty()  && channelRule.ChannelID.IsNull()  && channelRule.Type.IsNull()) return false;
            string strSql = new SQL().Update(ChannelRule._)
                .Set(ChannelRule._Status, channelRule.Status)
                .Set(ChannelRule._StatusDate, channelRule.StatusDate)
                .Set(ChannelRule._StatusResult, channelRule.StatusResult)
                .Set(ChannelRule._CreateTime, channelRule.CreateTime)
                .Set(ChannelRule._MaxSendCount, channelRule.MaxSendCount)
                .Set(ChannelRule._ManualOrderBy, channelRule.ManualOrderBy)
                .Set(ChannelRule._OrderNum, channelRule.OrderNum)
                .Set(ChannelRule._OrderNum2, channelRule.OrderNum2)
                .Set(ChannelRule._SuccessRate, channelRule.SuccessRate)
                .Set(ChannelRule._SendCount, channelRule.SendCount)
                .Set(ChannelRule._SendUser, channelRule.SendUser)
                .Set(ChannelRule._SendMoney, channelRule.SendMoney)
                .Set(ChannelRule._SpCount, channelRule.SpCount)
                .Set(ChannelRule._SpUser, channelRule.SpUser)
                .Set(ChannelRule._SpMoney, channelRule.SpMoney)
                .Set(ChannelRule._UpdateTime, channelRule.UpdateTime)
                .Set(ChannelRule._Ranking, channelRule.Ranking)
                .Where("1=1").Where(new Where()
                    .And(ChannelRule._CstCode, channelRule.CstCode, Operator.Equal, true)
                    .And(ChannelRule._CityCode, channelRule.CityCode, Operator.Equal, true)
                    .And(ChannelRule._ChannelID, channelRule.ChannelID, Operator.Equal, true)
                    .And(ChannelRule._Type, channelRule.Type, Operator.Equal, true)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) : Data.Pool(dbkey).ExecSql(strSql);
            if (value <= 0) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.ChannelRuleCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.ChannelRuleCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="channelRule">实体类</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(ChannelRule channelRule, string dbkey = "") {
            return Update(channelRule, dbkey, "", null);
        }
           /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="cstCode">通道控制台 运营商编号</param>
        /// <param name="cityCode">区号</param>
        /// <param name="channelID">通道编号</param>
        /// <param name="type">类型 1SMS 2IVR 3WAP</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">删除成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int? cstCode, string cityCode, int? channelID, int? type,  string dbkey = "", string where = "", string[] delCache = null) {
            if (cstCode.IsNull()  && cityCode.IsNullEmpty()  && channelID.IsNull()  && type.IsNull()) return false;
            string strSql = new SQL().Delete(ChannelRule._)
                .Where("1=1").Where(new Where()
                    .And(ChannelRule._CstCode, cstCode, Operator.Equal, true)
                    .And(ChannelRule._CityCode, cityCode, Operator.Equal, true)
                    .And(ChannelRule._ChannelID, channelID, Operator.Equal, true)
                    .And(ChannelRule._Type, type, Operator.Equal, true)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) :  Data.Pool(dbkey).ExecSql(strSql);
            if (value != 1) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.ChannelRuleCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.ChannelRuleCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="cstCode">通道控制台 运营商编号</param>
        /// <param name="cityCode">区号</param>
        /// <param name="channelID">通道编号</param>
        /// <param name="type">类型 1SMS 2IVR 3WAP</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int? cstCode, string cityCode, int? channelID, int? type, string dbkey = "") {
            return DeleteByID(cstCode, cityCode, channelID, type,  dbkey, "", null);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="cstCode">通道控制台 运营商编号</param>
        /// <param name="cityCode">区号</param>
        /// <param name="channelID">通道编号</param>
        /// <param name="type">类型 1SMS 2IVR 3WAP</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">删除成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int cstCode, string cityCode, int channelID, int type,  string dbkey = "", string where = "", string[] delCache = null) {
            return DeleteByID((int?)cstCode, (string)cityCode, (int?)channelID, (int?)type,  dbkey, where, delCache);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="cstCode">通道控制台 运营商编号</param>
        /// <param name="cityCode">区号</param>
        /// <param name="channelID">通道编号</param>
        /// <param name="type">类型 1SMS 2IVR 3WAP</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int cstCode, string cityCode, int channelID, int type, string dbkey = "") {
            return DeleteByID((int?)cstCode, (string)cityCode, (int?)channelID, (int?)type,  dbkey, "", null);
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <param name="where">附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="order">排序字段，不加“order by”</param>
        /// <param name="fieldList">设置需要返回的字段</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时随机取连接key</param>
        /// <returns>返回实体记录集</returns>
        public static IList<ChannelRule> SelectListByAll(string dbkey = "", string where = "", string order = "", string fieldList = "") {
            string cacheNameKey = "GTS.MiniBox.ChannelRuleCache_SelectListByAll_{0}_{1}_{2}".FormatWith(where, order, fieldList);
            return Cache2.Get<IList<ChannelRule>>(cacheNameKey, cacheSeconds, () => {
                string strSql = string.Empty;
			    if (fieldList.IsNullEmpty()) {
                    strSql = new SQL().From(ChannelRule._)
                        .Select(ChannelRule._CstCode)
                        .Select(ChannelRule._CityCode)
                        .Select(ChannelRule._ChannelID)
                        .Select(ChannelRule._Type)
                        .Select(ChannelRule._Status)
                        .Select(ChannelRule._StatusDate)
                        .Select(ChannelRule._StatusResult)
                        .Select(ChannelRule._CreateTime)
                        .Select(ChannelRule._MaxSendCount)
                        .Select(ChannelRule._ManualOrderBy)
                        .Select(ChannelRule._OrderNum)
                        .Select(ChannelRule._OrderNum2)
                        .Select(ChannelRule._SuccessRate)
                        .Select(ChannelRule._SendCount)
                        .Select(ChannelRule._SendUser)
                        .Select(ChannelRule._SendMoney)
                        .Select(ChannelRule._SpCount)
                        .Select(ChannelRule._SpUser)
                        .Select(ChannelRule._SpMoney)
                        .Select(ChannelRule._UpdateTime)
                        .Select(ChannelRule._Ranking)
                        .ToString();
                } else {
			        strSql = new SQL().From(ChannelRule._).Select(fieldList).ToString();
                }
                if (!where.IsNullEmpty()) strSql += " where 1=1 " + where;
                if (!order.IsNullEmpty()) strSql += " order by " + order;
                return Data.Pool(dbkey).GetDbDataReader(strSql).ToList<ChannelRule>();
            });
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>返回实体记录集</returns>
        public static IList<ChannelRule> SelectListByAll(string dbkey = "") {
            return SelectListByAll(dbkey, "", "", "");
        }
        /// <summary>
        /// 清除查询所有记录的缓存
        /// </summary>
        public static void ClearCacheSelectListByAll() {
            //Cache2.Remove("GTS.MiniBox.ChannelRuleCache_SelectListByAll___");
            Cache2.RemoveByPattern("GTS.MiniBox.ChannelRuleCache_SelectListByAll_(.+?)");
        }
        /// <summary>
        /// 清除所有缓存
        /// </summary>
        public static void ClearCacheAll() {
            Cache2.RemoveByPattern("GTS.MiniBox.ChannelRuleCache_(.+?)");
        }
    }
}

